package com.beadhouse.nurlevel.support.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.NurLevelEntity;
import com.beadhouse.nurlevel.ao.AddNurLevelAO;
import com.beadhouse.nurlevel.ao.UpdateNurLevelAO;
import com.beadhouse.nurlevel.dao.NurLevelDao;
import com.beadhouse.nurlevel.service.NurLevelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class NurLevelServiceImpl implements NurLevelService {
	NurLevelDao nldao;
	public NurLevelServiceImpl(NurLevelDao nldao) {
		this.nldao=nldao;
	}
	
	
	public NurLevelEntity selectOne(int NurlevelId) throws Exception {
		NurLevelEntity result=nldao.select("id",NurlevelId);
		return result;
	}
	public NurLevelEntity addNurLevel(AddNurLevelAO ao) throws Exception {
		NurLevelEntity e=new NurLevelEntity();
		e.setCreateDate(new Date());
		e.setDelFlag(0);
		e.setLevelName(ao.getLevelName());
		e.setLevelStatus(ao.getLevelStatus());
		e.setRemarks(ao.getRemarks());
		nldao.insert(e);
		return e;
	}
	public void updateNurLevel(int currentUserId ,UpdateNurLevelAO ao) throws Exception {
		int levelId=ao.getId();
		int createBy=ao.getCreateBy();
		String levelName=ao.getLevelName();
		Integer levelStatus=ao.getLevelStatus();
		String remark=ao.getRemarks();
		if(!nldao.exists("id", levelId)) {
			throw new BeadHouseException("数据不存在或已被删除");
		}
		Map<String,Object> map=new HashMap();
		NurLevelEntity old=nldao.select("id", levelId);
		if(levelName!=null) {
			if(!levelName.equals(old.getLevelName())) {
				map.put("levelName", levelName);
				old.setLevelName(levelName);
			}
		}
		if(levelStatus!=null) {
			if(!levelStatus.equals(old.getLevelStatus())) {
				map.put("levelName", levelName);
				old.setLevelName(levelName);
			}
		}
		if(remark!=null) {
			if(remark.equals(old.getRemarks())) {
				map.put("remark", remark);
				old.setRemarks(remark);
			}
		}
		
		map.put("updateDate", new Date());
		nldao.update(levelId, map);
	}
	
	public QueryResult<NurLevelEntity> selectLike(int pageNum,String key) throws Exception {
		
		PageHelper.startPage(pageNum,5);
		if (key != null) {
			key="%"+key+"%";
		}
		List<NurLevelEntity> s=nldao.selectLike(key);
		PageInfo<NurLevelEntity> page=new PageInfo<>(s);
		QueryResult<NurLevelEntity> qr=new QueryResult<>();
		qr.setPageNo(pageNum);
		qr.setPageSize(5);
		qr.setRows(s);
		qr.setTotalRows((int)page.getTotal());
		qr.getTotalPages();
		return qr;
	}
	
	@Override
	public QueryResult<NurLevelEntity> selectAll() throws Exception {
		String key=null;
		List<NurLevelEntity> s=nldao.selectLike(key);
		PageInfo<NurLevelEntity> page=new PageInfo<>(s);
		QueryResult<NurLevelEntity> qr=new QueryResult<>();
		qr.setPageSize(5);
		qr.setRows(s);
		qr.setTotalRows((int)page.getTotal());
		qr.getTotalPages();
		return qr;
	}
	
	public void delNurLevel(int levelId) throws Exception {
		Map<String,Object> map=new HashMap();
		map.put("delFlag", 1);
		map.put("id", levelId);
		nldao.update(levelId,map);
	}
	
}
